<?php

class Network {
	public $socket = null;

	function __construct() {
		$this->socket = null;
	}

	function __destruct() {
		if($this->socket != null) {
			socket_close($this->socket);
		}
	}

	function connect() {
//		echo "[connect]";
		$service_port = 2244;
		$address = gethostbyname("localhost");
		$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
		return socket_connect($this->socket, $address, $service_port);
	}

	function send($data) {
//		echo "send '$data'<br>";
		if($this->socket == null) {
			$this->connect();
		}
		socket_write($this->socket, $data, strlen($data));
	}

	function recv() {
		if($this->socket == null) {
			return NULL;
		}

		return rtrim(socket_read($this->socket, 2048, PHP_BINARY_READ), "\r\n");
	}
}

?>
